var messageEnum = {
    c2sInWaitingRomm: 1001,
    S2CloginMID: 1101,
    c2sReadyMID: 1002,
    s2cReadyMID: 1102,
    s2cGameStartMID: 1103,
    s2cRoundEndMID: 1104,
    c2sSendEnemy: 1005,
    s2cSendEnemyMID: 1105,
    c2sGameoverMID: 1006,
    s2cGameoverMID: 1106,
    c2sleakEnemyMID: 1007,
    s2cleakEnemyMID: 1107,
    c2sskillMID: 1008,
    s2cskillMID: 1108,
    s2cgetGoldMID: 1109,
    c2sToNextMID: 1010,
    s2cToNextMID: 1110,
    c2sInviteMID: 1011,
    ping: 50,
    pong: 51,
    ready: 1,
    fight: 2,
    build: 3,
};

class webNetworkManager {
	public isConnected:boolean;
	public constructor() {
	}
	/**心跳 */
	private hertBeatTimer: egret.Timer;
	public static instance: webNetworkManager;
	public static getInstance(): webNetworkManager {
		if (this.instance == null) {
			this.instance = new webNetworkManager();
		}
		return this.instance;
	}
	private heartTimer() {
		// this.hertBeatTimer = new egret.Timer(1000, -1);
		// this.hertBeatTimer.addEventListener(egret.TimerEvent.TIMER, e => {
		// 	if (this.isConnecteed == true)
		// 		webNetworkMananger.getInstance().SendMeesage('hert', {});
		// }, this);
		// this.hertBeatTimer.start();
	}
	//用来接受游戏场景的
	public gameSceneRec: any;
	public static test: boolean = false;
	/**服务器移动 */
	//private _root_url: string = "176.122.156.95";//bawagong
	private _root_url: string ="172.16.19.148";//wa
	//private _root_url: string = "172.16.19.190";
	//private _root_url: string = "172.16.19.104";
	public static isServerMove: boolean = false;
	public webSocket: egret.WebSocket;
	private heartMessag: egret.ByteArray;
	//CONCNET SERVER
	public connect(): void {
		if (this._root_url === null) {
			console.error("未设置连接HOST IP");
			return;
		}
		let heartMessagetmp = new awesomepackage.AwesomeMessage();
		heartMessagetmp.meesageId = 51;
		this.heartMessag = new egret.ByteArray(awesomepackage.AwesomeMessage.encode(heartMessagetmp).finish());
		// if (this.isConnecteed === true) {
		// 	console.error("服务器已经连接");
		// 	let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		// 	let m_c2sEnterRoom = new awesomepackage.c2sEnterRoom();
		// 	m_c2sEnterRoom.account = DataProl.account;
		// 	m_c2sEnterRoom.deessUp = JSON.stringify(DataProl.userCurrentDressUp);
		// 	m_AwesomeMessage.meesageId = 1002;
		// 	m_AwesomeMessage.data = awesomepackage.c2sEnterRoom.encode(m_c2sEnterRoom).finish();
		// 	var buffer =awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		// 	var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		// 	webNetworkMananger.getInstance().SendBitMeesage(byte2);
		// 	return;
		// }
		this.webSocket = new egret.WebSocket();
		//设置数据格式为二进制，默认为字符串
		this.webSocket.type = egret.WebSocket.TYPE_BINARY;
		//添加收到数据侦听，收到数据会调用此方法
		this.webSocket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
		//添加链接打开侦听，连接成功会调用此方法
		this.webSocket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this);
		//添加链接关闭侦听，手动关闭或者服务器关闭连接会调用此方法
		this.webSocket.addEventListener(egret.Event.CLOSE, this.onSocketClose, this);
		//添加异常侦听，出现异常会调用此方法
		this.webSocket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onSocketError, this);
		//连接服务器
		this.webSocket.connect(this._root_url, 3011);
	}


	public onSocketOpen(): void {
		this.isConnected = true;
		//	LoginMain.LoginBackMess=0;
		console.log("连接成功");
	}
	public onSocketClose(): void {
		this.isConnected = false;
		console.log("连接失败");
	}
	public onSocketError(): void {
		console.log("连接报错");
	}
	public SendBitMeesage(mes: egret.ByteArray): void {
		this.webSocket.writeBytes(mes);
		this.webSocket.flush();
	}
	//recive message
	public onReceiveMessage(e: egret.Event): void {
		var byte: egret.ByteArray = new egret.ByteArray();
		this.webSocket.readBytes(byte);
		this.reciveMessageHandle(byte.bytes);
	}
	/**显示错误信息 */
	public showErrorMessage(data: any) {
		this.gameSceneRec.displayErrorMessage(data);
	}
	/**登录 */
	public LoginGameServer() {
		let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
		let m_c2sLogin = new awesomepackage.c2sInWaitingRomm();
		m_c2sLogin.LoginName="player1";
		console.log(m_c2sLogin.LoginName);
		m_AwesomeMessage.meesageId = messageEnum.c2sInWaitingRomm;
		m_AwesomeMessage.data = awesomepackage.c2sInWaitingRomm.encode(m_c2sLogin).finish();
		var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
		var byte2: egret.ByteArray = new egret.ByteArray(buffer);
		webNetworkManager.getInstance().SendBitMeesage(byte2);
   }
   //点击开始，玩家准备状态
   public user_ready() {
		
	}
   /**接受数据 */
	public reciveMessageHandle(backData:any)
	{
     var pMessaage = awesomepackage.AwesomeMessage.decode(backData);
	 console.log(pMessaage.meesageId);
	 /**登录 */
	 if (pMessaage.meesageId == messageEnum.S2CloginMID) {
			let tmpMessage = awesomepackage.s2csInWaitingRomm.decode(pMessaage.data);
			//存到客户端
		  if (tmpMessage.isSucess) 
		  {
		  MsgeManage.userid = tmpMessage.userId;
		  MsgeManage.roomid = tmpMessage.roomId;
		  this.isConnected = true;
		  console.log("登录成功");
		  }
		  else
		  {
			  console.log("登录失败");
			  this.isConnected = false;
			  
		  }
             return;
		}
	 /**准备 */
	 if (pMessaage.meesageId == messageEnum.s2cReadyMID) {	
			return;
		}
		/**开始 */
		if (pMessaage.meesageId == messageEnum.s2cGameStartMID) {
			//初始化回合数 
			return;
		}

	}
}